package com.shemy.sshiro.realm;

import com.shemy.sshiro.domain.SysMenu;
import com.shemy.sshiro.domain.SysRole;
import com.shemy.sshiro.domain.SysUser;
import com.shemy.sshiro.service.SysMenuService;
import com.shemy.sshiro.service.SysRoleService;
import com.shemy.sshiro.service.SysUserService;
import com.shemy.sshiro.utils.JwtToken;
import com.shemy.sshiro.utils.JwtUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description
 * @Author Dzsom
 * @Date 2019-05-14 9:37
 * @Encoding UTF-8
 * @Version 1.0
 **/
@Component
public class JWTShiroRealm extends AuthorizingRealm {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 必须重写此方法，不然Shiro会报错！
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 权限认证
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String username = JwtUtil.getUsername(principalCollection.toString());
        SysUser user = sysUserService.findByName(username);
        List<SysRole> roles = sysRoleService.findByUserId(user.getUserId());
        List<SysMenu> permissions = sysMenuService.findAllByRoles(roles);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (SysRole role:roles){
            simpleAuthorizationInfo.addRole(role.getRoleName());
        }
        for (SysMenu permission : permissions) {
            simpleAuthorizationInfo.addStringPermission(permission.getMenuName());
        }
        return simpleAuthorizationInfo;
    }

    /**
     * 身份认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String token = (String) authenticationToken.getCredentials();
        String username = JwtUtil.getUsername(token);
        if (username==null){
            throw new AuthenticationException("身份失效！");
        }

        SysUser user = sysUserService.findByName(username);
        if (user==null){
            throw new AuthenticationException("用户不存在!");
        }
        if (!JwtUtil.verify(token,user.getAccount(),user.getPassword())){
            throw new AuthenticationException("用户名或密码错误");
        }
        return new SimpleAuthenticationInfo(token,token,this.getName());
    }
}
